package com.ridaizu.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.google.common.base.Strings;
import com.ridaizu.cache.CacheDataBean;
import com.ridaizu.dao.db.DBManager;
import com.ridaizu.lazy.LazyGoodsManagerDataModel;
import com.ridaizu.model.GoodsManager;
import com.ridaizu.model.form.GoodsManagerForm;
import com.ridaizu.model.search.GoodsManagerSearch;
import com.ridaizu.service.impl.GoodsManagerServiceImpl;
import com.ridaizu.util.DateUtil;

@javax.faces.bean.ManagedBean(name = "goodsManagerBean")
@SessionScoped
public class GoodsManagerBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private LazyDataModel<GoodsManager> lazyModel;

    @ManagedProperty("#{cacheDataBean}")
    private CacheDataBean cacheDataBean;

    @ManagedProperty("#{goodsManagerServiceImpl}")
    private GoodsManagerServiceImpl goodsManagerService;

    @ManagedProperty("#{goodsManagerSearch}")
    private GoodsManagerSearch goodsManagerSearch;

    @ManagedProperty("#{goodsManagerForm}")
    private GoodsManagerForm goodsManagerForm;

	public void searchGoodsManagerSync() {
		lazyModel = new LazyGoodsManagerDataModel(goodsManagerSearch);
	}

	public void viewRegisterGoodsManager() throws SQLException {

		setInitValues(null);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 500);

        RequestContext.getCurrentInstance().openDialog("goods-manager", options, null);
    }

	public void viewUpdateGoodsManager(GoodsManager goodsManager) throws SQLException {

		setInitValues(goodsManager);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 520);

        RequestContext.getCurrentInstance().openDialog("goods-manager", options, null);
    }

	private void setInitValues(GoodsManager goodsManager) throws SQLException {
		goodsManagerForm.clearAll();
		setValues(goodsManager);
	}

	private void setValues(GoodsManager goodsManager) {
		if (goodsManager == null) {
			setRegisterValues();
		} else {
			setUpdateValues(goodsManager);
		}
	}

	private void setRegisterValues() {
	}

	private void setUpdateValues(GoodsManager goodsManager) {
		goodsManagerForm.setId(String.valueOf(goodsManager.getId()));
		goodsManagerForm.setBrandId(String.valueOf(goodsManager.getBrandId()));
		goodsManagerForm.setBrandName(String.valueOf(goodsManager.getBrandName()));
		goodsManagerForm.setBrandSeriesId(String.valueOf(goodsManager.getBrandSeriesId()));
		goodsManagerForm.setBrandSeriesName(String.valueOf(goodsManager.getBrandSeriesName()));
		goodsManagerForm.setGoodsId(String.valueOf(goodsManager.getGoodsId()));
		goodsManagerForm.setGoodsName(String.valueOf(goodsManager.getGoodsName()));
		goodsManagerForm.setRgtOpt(goodsManager.getRgtOpt());
		goodsManagerForm.setRgtDate(new Date(goodsManager.getRgtDate().getTime()));
		goodsManagerForm.setUpdOpt(goodsManager.getUpdOpt());
		goodsManagerForm.setUpdDate(new Date(goodsManager.getUpdDate().getTime()));
	}

	public void registerGoodsManager() {
		GoodsManager goodsManager = getGoodsManager();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsManagerService.insertModel(goodsManager);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
    }

	public void updateGoodsManager() {

		GoodsManager goodsManager = getGoodsManager();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsManagerService.updateModel(goodsManager);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
	}

	public void deleteGoodsManager() {

		GoodsManager selectedGoodsManager = goodsManagerForm.getSelectedGoodsManager() ;

		if (selectedGoodsManager == null || selectedGoodsManager.getId() < 1) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "请选择要删除的商品管理信息。");
	        FacesContext.getCurrentInstance().addMessage(null, message);

	        return ;
		}

		boolean result = false;
		try {
			DBManager.beginTransaction();
			result = goodsManagerService.deleteModel(selectedGoodsManager.getId());
			DBManager.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		String resultStr = null;
		if (result) {
			resultStr = String.format("商品管理[%s]删除成功。", selectedGoodsManager.getId());
		} else {
			resultStr = String.format("商品管理[%s]删除失败。", selectedGoodsManager.getId());
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "GoodsManager delete result", resultStr);
        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void registerGoodsManagerResult(SelectEvent event) {
		boolean result = (Boolean) event.getObject();

		handleResult(result, "登录");
	}

	public void updateGoodsManagerResult(SelectEvent event) {

		boolean result = (Boolean) event.getObject();

		handleResult(result, "更新");
	}

	private void handleResult(boolean result, String optStr) {

		String resultStr = null;
		if (result) {
			resultStr = String.format("商品管理信息%s成功。", optStr);
		} else {
			resultStr = String.format("商品管理信息%s失败。", optStr);;
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", resultStr);

        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private GoodsManager getGoodsManager() {

		GoodsManager goodsManager = new GoodsManager();

		if (!Strings.isNullOrEmpty(goodsManagerForm.getId())) {
			goodsManager.setId(Integer.valueOf(goodsManagerForm.getId()));
		}

		if (!Strings.isNullOrEmpty(goodsManagerForm.getBrandId())) {
			goodsManager.setBrandId(Integer.valueOf(goodsManagerForm.getBrandId()));
		}

		if (!Strings.isNullOrEmpty(goodsManagerForm.getBrandSeriesId())) {
			goodsManager.setBrandSeriesId(Integer.valueOf(goodsManagerForm.getBrandSeriesId()));
		}

		if (!Strings.isNullOrEmpty(goodsManagerForm.getGoodsId())) {
			goodsManager.setGoodsId(Integer.valueOf(goodsManagerForm.getGoodsId()));
		}

		Timestamp currentDate = DateUtil.getCurrentDateTime();

		goodsManager.setRgtOpt("Admin");
		goodsManager.setRgtDate(currentDate);
		goodsManager.setUpdOpt("Admin");
		goodsManager.setUpdDate(currentDate);

		return goodsManager;
	}

	public LazyDataModel<GoodsManager> getLazyModel() {
		return lazyModel;
	}

	public void setLazyModel(LazyDataModel<GoodsManager> lazyModel) {
		this.lazyModel = lazyModel;
	}

	public GoodsManagerServiceImpl getGoodsManagerService() {
		return goodsManagerService;
	}

	public void setGoodsManagerService(GoodsManagerServiceImpl goodsManagerService) {
		this.goodsManagerService = goodsManagerService;
	}

	public GoodsManagerSearch getGoodsManagerSearch() {
		return goodsManagerSearch;
	}

	public void setGoodsManagerSearch(GoodsManagerSearch goodsManagerSearch) {
		this.goodsManagerSearch = goodsManagerSearch;
	}

	public GoodsManagerForm getGoodsManagerForm() {
		return goodsManagerForm;
	}

	public void setGoodsManagerForm(GoodsManagerForm goodsManagerForm) {
		this.goodsManagerForm = goodsManagerForm;
	}

	public CacheDataBean getCacheDataBean() {
		return cacheDataBean;
	}

	public void setCacheDataBean(CacheDataBean cacheDataBean) {
		this.cacheDataBean = cacheDataBean;
	}

}